package com.example.remoteviewdemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

public class ActivityA extends Activity {

    private LinearLayout mRemoteViewContent;
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            RemoteViews remoteViews = intent.getParcelableExtra("remoteviews");
            if (remoteViews != null) {
                updateUI(remoteViews);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ActivityA.this, ActivityB.class);
                startActivity(intent);
            }
        });
        initView();
    }
    private void initView() {
        mRemoteViewContent = (LinearLayout) findViewById(R.id.ll_remoteviews);
        IntentFilter filter = new IntentFilter("gaga");
        registerReceiver(mBroadcastReceiver, filter);
    }

    private void updateUI(RemoteViews remoteViews) {
       //View view = remoteViews.apply(this, mRemoteViewContent);

        int layoutId = getResources().getIdentifier("notification", "layout", getPackageName());
        View view = getLayoutInflater().inflate(layoutId, mRemoteViewContent, false);
        mRemoteViewContent.addView(view);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }
}
